﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace GestaltDesigner
{
    public partial class PropertyGrid : UserControl
    {
        public PropertyGrid()
        {
            InitializeComponent();
            this.KeyDown += new KeyEventHandler(PropertyGrid_KeyDown);
        }

        void PropertyGrid_KeyDown(object sender, KeyEventArgs e)
        {
            //try to move the focus to the textbox below
            
            if (e.Key == Key.Enter)
            {
                try
                {
                    TextBox origTextblock= e.OriginalSource as TextBox;
                   
                    StackPanel panel = origTextblock.Parent as StackPanel;
                    StackPanel parentPanel = panel.Parent as StackPanel;
                    int position = parentPanel.Children.IndexOf(panel);
                    StackPanel newPanel = parentPanel.Children[position + 1] as StackPanel;
                    TextBox newTextblock = newPanel.Children[1] as TextBox;
                    if (newTextblock != null)
                        newTextblock.Focus();
                }
                catch (ArgumentOutOfRangeException)
                {
                }
            }
            //want to bubble event so prop change can fire;
            e.Handled = false;
        }
    }
    public class PropertyGridDataObject : INotifyPropertyChanged    
    {
        private FrameworkElement backingControl;

        public FrameworkElement BackingControl
        {
            get { return backingControl; }
            set { 

                backingControl = value;
                this.Name = backingControl.Name;
                this.Height = backingControl.Height;
                this.Width = backingControl.Width;
                this.Opacity = backingControl.Opacity;
                this.Margin = backingControl.Margin;
                this.Left = (double)backingControl.GetValue(Canvas.LeftProperty);
                this.Top = (double)backingControl.GetValue(Canvas.TopProperty);
                if (backingControl is ContentControl)
                {
                    this.Content = ((ContentControl)backingControl).Content.ToString();
                    
                }
                if (backingControl  is TextBox)
                    this.Text = ((TextBox)backingControl).Text;
                if (backingControl is TextBlock)
                    this.Text = ((TextBlock)backingControl).Text;
                if (backingControl is Image)
                    this.Src = ((Image)backingControl).Source;
                if (backingControl is MediaElement)
                {
                    this.MediaSrc = ((MediaElement)backingControl).Source;
                    this.AutoPlay = ((MediaElement)backingControl).AutoPlay;
                }
                if (backingControl is MultiScaleImage)
                    this.DeepzoomSrc = ((MultiScaleImage)backingControl).Source as DeepZoomImageTileSource;
                NotifyPropertyChanged("BackingControl");
            }
        }


        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                backingControl.Name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private double height;

        public double Height
        {
            get { return height; }
            set { height = value;
            backingControl.Height = value;
            NotifyPropertyChanged("Height");

            }
        }
        private double width;

        public double Width
        {
            get { return width; }
            set { width = value;
            backingControl.Width = value;
            NotifyPropertyChanged("Width");

            }
        }
        private double left;

        public double Left
        {
            get { return left; }
            set { left = value;
            backingControl.SetValue(Canvas.LeftProperty, value);
            NotifyPropertyChanged("Left");

            }
        }
        private double top;

        public double Top
        {
            get { return top; }
            set { top = value;
            backingControl.SetValue(Canvas.TopProperty, value);
            NotifyPropertyChanged("Top");

            }
        }
        private Thickness margin;

        public Thickness Margin
        {
            get { return margin; }
            set { margin = value;
            backingControl.Margin = value;
            NotifyPropertyChanged("Margin");

            }
        }
        private double opacity;

        public double Opacity
        {
            get { return opacity; }
            set { opacity = value;
            backingControl.Opacity = value;
            NotifyPropertyChanged("Opacity");

            }
        }
        private string content;

        public string Content
        {
            get { return content; }
            set { content = value;
            if (backingControl is ContentControl)
                ((ContentControl)backingControl).Content = value;
            NotifyPropertyChanged("Content");
            
            }
        }
        private string text;

        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                if (backingControl is TextBox)
                    ((TextBox)backingControl).Text = value;
                if (backingControl  is TextBlock)
                    ((TextBlock)backingControl).Text = value;
                    
                    
                    NotifyPropertyChanged("Text");

            }
        }

        private ImageSource src;
        

        public ImageSource Src
        {
            get {
                if (src != null)
                {
                    BitmapImage img = (BitmapImage)src;
                    if (img.UriSource.OriginalString  == "")
                    {
                        Uri uri;
                        uri = new Uri("...", UriKind.RelativeOrAbsolute);
                        return new BitmapImage(uri);
                    }
                }
                return src; 
            
            }
            set { src = value;
            if (backingControl is Image)
                ((Image)backingControl).Source = value;
            NotifyPropertyChanged("Src");

            
            }
        }

        private Uri mediaSrc = new Uri("...",UriKind.RelativeOrAbsolute);

        public Uri MediaSrc
        {
            get {
               
                  return mediaSrc; 

            }
            set { mediaSrc = value;
            if (backingControl is MediaElement)
                ((MediaElement)backingControl).Source = value;
            NotifyPropertyChanged("MediaSrc");

            }
        }

        private bool autoPlay = true;

        public bool AutoPlay
        {
            get { return autoPlay; }
            set { autoPlay = value;
            if (backingControl is MediaElement)
                ((MediaElement)backingControl).AutoPlay = value;
            NotifyPropertyChanged("AutoPlay");
            
            }
        }

        private DeepZoomImageTileSource deepzoomSrc;

        public DeepZoomImageTileSource DeepzoomSrc
        {
            get { return deepzoomSrc; }
            set { deepzoomSrc = value; 
            if (backingControl is MultiScaleImage)
                ((MultiScaleImage)backingControl).Source = value;
            NotifyPropertyChanged("DeepzoomSrc");
            
            }
        }

        // Declare the PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;

        // NotifyPropertyChanged will raise the PropertyChanged event passing the
        // source property that is being updated.
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }  

    }
}
